import request from '../../utils/request'
import type { CreateProjectRequest, NoneResponse, GetAllProjectResponse, GetJoinedProjectResponse, GetProjectByIdResponse, GetMemberResponse } from './type'

enum API {
  HANDLERPROJECT_URL = '/project',
  GETMEMBERPROJECT_URL = 'project/member',
  HANDLERPROJECTBYID_URL = '/project/{projectId}',
  HANDLERMEMBERPROJECTBYID_URL = '/project/member/{projectId}',
  CHANGEMEMBERROLE_URL = '/project/member/{projectId}/{userId}/{projectRole}',
  CHANGECREADER_URL = '/project/creator/{projectId}/{transferTo}',
  REMOVEMEMBER_URL = '/project/member/{projectId}/{userId}',
}

// 新建项目
export const reqCreateProject = (data: CreateProjectRequest) => request.post<CreateProjectRequest, NoneResponse>(API.HANDLERPROJECT_URL, data)
// 获取所有项目
export const reqGetProject = (page:number, pageSize:number) => request.get<number, GetAllProjectResponse>(API.HANDLERPROJECT_URL, {params: {page, pageSize}})
// 获取已加入项目
export const reqGetMemberProject = () => request.get<any, GetJoinedProjectResponse>(API.GETMEMBERPROJECT_URL)
// 根据id获取项目
export const reqGetProjectById = (projectId: number) => request.get<any, GetProjectByIdResponse>(API.HANDLERPROJECTBYID_URL.replace('{projectId}', projectId.toString()))
// 根据id更新项目
export const reqUpdateProjectById = (projectId: number, data:CreateProjectRequest) => request.put<CreateProjectRequest, NoneResponse>(API.HANDLERPROJECTBYID_URL.replace('{projectId}', projectId.toString()), data)
// 根据id删除项目
export const reqDeleteProjectById = (projectId: number) => request.delete<any, NoneResponse>(API.HANDLERPROJECTBYID_URL.replace('{projectId}', projectId.toString()))
// 获取项目的所有成员
export const reqGetAllMemberProjectById = (projectId: number, page?:number, pageSize?:number) => request.get<number, GetMemberResponse>(API.HANDLERMEMBERPROJECTBYID_URL.replace('{projectId}', projectId.toString()), {params: {page, pageSize}})
// 加入项目
export const reqJoinMemberProjectById = (projectId: number) => request.post<any, NoneResponse>(API.HANDLERMEMBERPROJECTBYID_URL.replace('{projectId}', projectId.toString()))
// 退出项目
export const reqExitMemberProjectById = (projectId: number) => request.delete<any, NoneResponse>(API.HANDLERMEMBERPROJECTBYID_URL.replace('{projectId}', projectId.toString()))
// 更改成员角色
export const reqChangeMemberRoleById = (projectId: number, userId: number, projectRole: string) => request.put<any, NoneResponse>(API.CHANGEMEMBERROLE_URL.replace('{projectId}', projectId.toString()).replace('{userId}', userId.toString()).replace('{projectRole}', projectRole.toString()))
// 转让项目
export const reqChangeReaderById = (projectId: number, transferTo: number) => request.put<any, NoneResponse>(API.CHANGECREADER_URL.replace('{projectId}', projectId.toString()).replace('{transferTo}', transferTo.toString()))
// 移出用户
export const reqRemoveMember = (projectId:number, userId:number) => request.delete<any, NoneResponse>(API.REMOVEMEMBER_URL.replace('{projectId}', projectId.toString()).replace('{userId}', userId.toString()))